<?php
require('../../inc/init.php');
require_once getCacheFilePath('forums.php');

$action=strFilter($_GET["action"]);
switch($action){
	case "new":

/*新帖*/
$newtopicstr='';
$newtopics=$db->row_select("topics","verifystate=0 and isrecycle=0",10,"id,fid,title,userid,username,highlight,posttime,ordertime,lastreply","ordertime desc");
$newi=0;
foreach($newtopics as $newtopic){
	$newi++;
	$newtopic['title']=htmlFilter($newtopic['title']);
	$newtopic['username']=htmlFilter($newtopic['username']);
	if(!empty($newtopic['highlight'])){
		$newtopic['title']=genHighLighStr($newtopic['highlight'],$newtopic['title']);
	}
	if($newtopic['posttime']==$newtopic['ordertime']){
		$linktitle="{$newtopic['username']} 发表于 ".getDateStr($newtopic['posttime']);
		$newtopic['posttime']=getDateStr($newtopic['posttime']);
		$newtopicstr.="<li class='normaltitle' title=\"{$linktitle}\"><a href='../../user.php?uid={$newtopic['userid']}' target='_blank'>{$newtopic['username']}</a> 发表了 <a href=\"../../view.php?tid={$newtopic['id']}\" target=\"_blank\">{$newtopic['title']}</a></li>";
	}else{
		$lastre=explode("\t",$newtopic['lastreply']);
		$linktitle=htmlFilter($lastre[1])." 回复于 ".getDateStr($lastre[0]);
		$newtopicstr.="<li class='normaltitle' title=\"{$linktitle}\"><a href='../../user.php?uid={$newtopic['userid']}' target='_blank'>{$newtopic['username']}</a> 回复了 <a href=\"../../view.php?tid={$newtopic['id']}\" target=\"_blank\">{$newtopic['title']}</a></li>";
	}
}

$activeuserstr='';
$activeusers=$db->row_select("users","groupid<100 or groupid>200",9,"id,username,lastposttime","lastposttime desc");
foreach($activeusers as $activeuser){
	$activeuser['lastposttime']=getDateStr($activeuser['lastposttime']);
	$activeuser['userurl']="../../user.php?uid={$activeuser['id']}";
	$activeuser['username']=htmlFilter($activeuser['username']);
	$activeuserstr.=<<<EOT
	<li><a href="{$activeuser['userurl']}" title="{$activeuser['username']} 于 {$activeuser['lastposttime']} 发帖" target="_blank"><img src="../../userpic.php?type=small&uid={$activeuser['id']}" border="0" /></a><span><a href="{$activeuser['userurl']}" title="{$activeuser['username']} 于 {$activeuser['lastposttime']} 发帖" target="_blank">{$activeuser['username']}</a></span></li>
EOT;
}

print<<<EOT
<table class="wintable"><tr><td class="left"><span class="title">最近发布的帖子</span>
<div class="hotnew"><ul>{$newtopicstr}</ul></div>
</td><td class="middle"></td><td class="right"><div class="activetitle">活跃分子</div><div class="index_right_newmember"><ul>{$activeuserstr}</ul></div>
</td></tr></table>
EOT;
	break;

	case "online":
/*动静*/
$onlinerowstr='';
$onlineuserstr='';
$onlinerows=$db->row_query("Select * From (Select (case When userid=0 then 0 else 1 end) As user_mark,{$db->pre}online.* From {$db->pre}online) {$db->pre}online Order By user_mark desc,activetime desc limit 10");
foreach($onlinerows as $onlinerow){
		$onlinerow['activetime']=getDateStr($onlinerow['activetime']);
		$forum=$cache_forums[$onlinerow['fid']];
		$onlinerow['name']=$onlinerow['userid']==0?"游客":"<a href='".genUrl("user.php?uid={$onlinerow['userid']}")."' target='_blank'>{$onlinerow['username']}</a>";
		$onlinerow['ip']=$lg['groupid']<GROUP_FORUMADMIN?"--":"<a href='".str_replace('{ip}',$onlinerow['ip'], $cache_settings['iplink'])."' target='_blank'>{$onlinerow['ip']}</a>";
		$onlinerow['position']='';
		$onlinerow['doaction']='';
		switch($onlinerow['atpage']){
			case 'forum':
				$onlinerow['title']="浏览版块 <a href='../../forum.php?fid={$onlinerow['fid']}' target='_blank'>{$cache_forums[$onlinerow['fid']]['forumname']}</a>";
			break;

			case 'view':
				$onlinerow['title']="在 <a href='../../forum.php?fid={$onlinerow['fid']}' target='_blank'>{$cache_forums[$onlinerow['fid']]['forumname']}</a> 浏览帖子 <a href='".genUrl("view.php?tid={$onlinerow['tid']}")."' target='_blank'>#".$onlinerow['tid']."</a>";
			break;

			case 'online':
				$onlinerow['title']='查看在线用户列表';
			break;

			case 'member':
				$onlinerow['title']='浏览个人中心';
			break;

			case 'login':
				$onlinerow['title']='准备登录';
			break;
		
			case 'search':
				$onlinerow['title']='搜索帖子';
			break;
		
			case 'userlist':
				$onlinerow['title']='查看会员列表';
			break;

			case 'index':
				$onlinerow['title']='浏览首页';
			break;

			case 'signup':
				$onlinerow['title']='查看注册页面';
			break;
		
			case 'post':
				$onlinerow['title']="在 <a href='../../forum.php?fid={$onlinerow['fid']}' target='_blank'>{$cache_forums[$onlinerow['fid']]['forumname']}</a> 发布帖子";
			break;
			
			case 'announce':
				$onlinerow['title']='查看公告';
			break;
		
		}

		$onlinerow['onlineicon']=0;
		if($onlinerow['groupid']>200){
			$onlinerow['onlineicon']=$onlinerow['groupid'];
		}elseif($onlinerow['groupid']>100){
			$onlinerow['onlineicon']=0;
		}else{
			$onlinerow['onlineicon']=1;
		}

		$onlinerowstr.=<<<EOT
<tr><td class="onlinetitle"><img src="../../images/group/{$onlinerow['onlineicon']}.gif" border="0" align="absmiddle" /> {$onlinerow['name']} <span class="time">{$onlinerow['activetime']}</span>&nbsp;{$onlinerow['title']}&nbsp;</td></tr>
EOT;
	if(intval($onlinerow['userid'])>0){
	$onlineuserstr.=<<<EOT
	<li><a href="../../user.php?uid={$onlinerow['userid']}" target="_blank"><img src="../../userpic.php?type=small&uid={$onlinerow['userid']}" border="0" /></a><span><a href="../../user.php?uid={$onlinerow['userid']}" target="_blank">{$onlinerow['username']}</a></span></li>
EOT;
	}

}
print<<<EOT
		<table class="wintable"><tr><td class="left" style="width:540px;"><span class="title">看看别人的动静</span>
<table width="100%">{$onlinerowstr}</table>
		</td></tr></table>
EOT;
	break;

	case "hot":
/*热门*/
$hottopicstr='';
$hottopics=$db->row_select("topics","verifystate=0 and isrecycle=0",10,"*","replynum desc");
foreach($hottopics as $hottopic){
	$hottopic['title']=htmlFilter($hottopic['title']);
	$hottopic['username']=htmlFilter($hottopic['username']);
	if(!empty($hottopic['highlight'])){
		$hottopic['title']=genHighLighStr($hottopic['highlight'],$hottopic['title']);
	}
	$linktitle="{$hottopic['username']} 发表于 {$hottopic['posttime']}";
	$hottopic['posttime']=getDateStr($hottopic['posttime']);
	$hoticon=getTopicIcon($hottopic);
	$hottopicstr.="<tbody><tr><td class='topictype'><img src='../../images/topic/{$hoticon[0]}.gif' border='0' title='{$hoticon[1]}' /></td><td class='hottitle'><a href=\"../../view.php?tid={$hottopic['id']}\" target=\"_blank\">{$hottopic['title']}</a></td><td class='replyhits'><span class='replynum'>{$hottopic['replynum']}</span> / {$hottopic['hits']}</td></tr></tbody>";
}

print<<<EOT
		<table class="wintable"><tr><td class="left" style="width:540px;">
<table width="100%" class="hottable">
<thead><tr><td class="topictype">类型</td><td class="hottitle">标题</td><td class="replyhits">回复 / 点击</td></tr></thead>
{$hottopicstr}
</table>
		</td></tr></table>
EOT;
	break;

	case "rank":
/*排行*/
$rank1str='';
$rank1rows=$db->row_select("users","groupid<100 or groupid>200",12,"id,username,postsnum","postsnum desc");
foreach($rank1rows as $rank1row){
	$rank1row['userurl']="../../user.php?uid={$rank1row['id']}";
	$rank1row['username']=htmlFilter($rank1row['username']);
	$rank1str.=<<<EOT
	<li title="发帖数:{$rank1row['postsnum']}"><a href="{$rank1row['userurl']}" target="_blank"><img src="../../userpic.php?type=small&uid={$rank1row['id']}" border="0" /></a><span><a href="{$rank1row['userurl']}" target="_blank">{$rank1row['username']}</a></span></li>
EOT;
}

$rank2str='';
$rank2rows=$db->row_query("select ({$cache_settings['creditsexpression']}) as C,id,username from `{$db->pre}users` order by C desc limit 12");
foreach($rank2rows as $rank2row){
	$rank2row['userurl']="../../user.php?uid={$rank2row['id']}";
	$rank2row['username']=htmlFilter($rank2row['username']);
	$rank2str.=<<<EOT
	<li title="总积分:{$rank2row['C']}"><a href="{$rank2row['userurl']}" target="_blank"><img src="../../userpic.php?type=small&uid={$rank2row['id']}" border="0" /></a><span><a href="{$rank2row['userurl']}" target="_blank">{$rank2row['username']}</a></span></li>
EOT;
}

$rank3str='';
$rank3rows=$db->row_query("select count(0) as C,userid,fanname from `{$db->pre}fans` group by userid order by C desc limit 12");
$rank3users=array();
foreach($rank3rows as $rank3row){
	array_push($rank3users,$rank3row['userid']);
}
$userrows=array();
$usernames=array();
if(!empty($rank3users)){
	$userrows=$db->row_query("select id,username from `{$db->pre}users` where id in (".implode(",",$rank3users).")");
	foreach($userrows as $userrow){
		$usernames["{$userrow['id']}"]=$userrow['username'];
	}
}

foreach($rank3rows as $rank3row){
	$rank3row['userurl']="../../user.php?uid={$rank3row['userid']}";
	$rank3row['fanname']=htmlFilter($rank3row['fanname']);
	$rank3str.=<<<EOT
	<li title="有 {$rank3row['C']} 个粉丝"><a href="{$rank3row['userurl']}" target="_blank"><img src="../../userpic.php?type=small&uid={$rank3row['userid']}" border="0" /></a><span><a href="{$rank3row['userurl']}" target="_blank">{$usernames[$rank3row['userid']]}</a></span></li>
EOT;
}

print<<<EOT
<table class="wintable"><tr>
<td class="ranktd">
<span class="title">发帖排行榜</span>
<div class="index_right_newmember"><ul>{$rank1str}</ul></div>
</td><td class="middle"></td>
<td class="ranktd">
<span class="title">积分排行榜</span>
<div class="index_right_newmember"><ul>{$rank2str}</ul></div>
</td><td class="middle"></td>
<td class="ranktd">
<span class="title">最受欢迎</span>
<div class="index_right_newmember"><ul>{$rank3str}</ul></div>
</td>
</tr></table>
EOT;
	break;
}
?>